package com.nf.health.app.main_intervention.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.bodyfat.bean.BodyFatInfo;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.http.Act;
import com.nf.health.app.main_intervention.bean.MeridianBean;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 执行方案： 经络
 * @author yuyahao
 */
public class MeridianActivity  extends BaseActivity{
    private   String ID = "";
    /*@Bind(R.id.tv_suggest_descript2)
        TextView tv_suggest_descript2;*/
    @Bind(R.id.tv_suggest_tip_meridian)
    TextView tv_suggest_tip_meridian;
    @Bind(R.id.tv_suggest_time_meridian)
    TextView tv_suggest_time_meridian;
    @Bind(R.id.iv_show_img_meridian)
    ImageView iv_show_img_meridian;
    @Bind(R.id.tv_of_meridian_type)
    TextView tv_of_meridian_type;
    @Bind(R.id.lc_meridian)
    MyListView lc_meridian;
    private  List<MyhealthySign> list;
    private MyhealthySignAdapter myhealthySignAdapter ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_meridian);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "执行方案");
        initListener();
        initView();
    }

    private void initView() {
        BodyFatInfo bodyFatInfo = new BodyFatInfo();
        String success_tip = bodyFatInfo.getBone();//设置底部的建议提示
        String rl_music_title = bodyFatInfo.getBone();//干预方案类型
        String tv_suggest_timeStr  = bodyFatInfo.getBone();//设置建议的时间
        String st_of_meridian_type = "";
        if(TextUtils.isEmpty(st_of_meridian_type)){
            st_of_meridian_type = "-24式太极拳-";
        }
        tv_of_meridian_type.setText(st_of_meridian_type);

        if(TextUtils.isEmpty(rl_music_title)){
            rl_music_title = "经络方案";
        }
       if(TextUtils.isEmpty(tv_suggest_timeStr)){
            tv_suggest_timeStr = "建议时间：9:30 上午";
        }
       // tv_suggest_descript2.setText(success_tip);
        tv_suggest_tip_meridian.setText("建议时间："+rl_music_title);
        tv_suggest_time_meridian.setText(tv_suggest_timeStr);
        GlideUtil.loadNetImg2(MeridianActivity.this,iv_show_img_meridian,bodyFatInfo.getbMR(),R.drawable.default_image);
        list = new ArrayList<>();
        myhealthySignAdapter = new MyhealthySignAdapter(MeridianActivity.this,list);
        lc_meridian.setAdapter(myhealthySignAdapter);
        ID =getIntent().getStringExtra(Constants.INTERVENTION_ID);
    }

    private void initListener() {
    }
    @Override
    protected void onResume() {
        super.onResume();
        loadData(ID);
    }
    private void loadData(String id){
        provider.getInterveneMeridianById(id);
    }
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case Act.Flag.GETINTERVENEMERIDIANBYID_FLAG:
                MeridianBean meridianBean = (MeridianBean) object;
                list.clear();
                if(meridianBean != null){
                    for (int i = 0; i < 3; i++) {
                        MyhealthySign m = new MyhealthySign();
                        switch (i){
                            case 0:
                                if(!TextUtils.isEmpty(meridianBean.getAct())){
                                    m.setTitle("取穴位置");
                                    m.setContext(""+meridianBean.getPosition());
                                    list.add(m);
                                }
                                break;
                            case 1:
                                if(!TextUtils.isEmpty(meridianBean.getAct())){
                                    m.setTitle("动作要领");
                                    m.setContext(""+meridianBean.getAct());
                                    list.add(m);
                                }
                                break;
                            case 2:
                                if(!TextUtils.isEmpty(meridianBean.getSuggest())){
                                    m.setTitle("建议");
                                    m.setContext(""+meridianBean.getSuggest());
                                    list.add(m);
                                }
                                break;
                        }
                    }
                    myhealthySignAdapter.setMyNotifyDataChanged(list);
                    tv_of_meridian_type.setText(TextUtils.isEmpty(meridianBean.getName()) ? "" : meridianBean.getName());
                    GlideUtil.loadNetImg2(MeridianActivity.this,iv_show_img_meridian,meridianBean.getPicture(),R.drawable.default_image);
                    tv_suggest_time_meridian.setText(TextUtils.isEmpty(meridianBean.getTime()) ? "建议时间：暂无" : "建议时间：" +meridianBean.getTime()+" "+meridianBean.getHour());
                }
                break;
        }
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.aaa:
                break;
        }
    }
    private class MyhealthySignAdapter extends BaseAdapter{
        private List<MyhealthySign> list;
        private Context cnt;
        private LayoutInflater layoutInflater;
        private int selectIndex;//表示选中的搜索的索引值
        public MyhealthySignAdapter(Context cnt, List<MyhealthySign> list){
            this.cnt = cnt;
            this.list = list;
            layoutInflater = LayoutInflater.from(cnt);
        }
        public void setMyNotifyDataChanged ( List<MyhealthySign>list){
            this.list = list;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return  list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.item_heanthy_sign, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_sub_content = (TextView) convertView.findViewById(R.id.tv_sub_content);
                viewHolder.tv_cu_tl = (TextView) convertView.findViewById(R.id.tv_cu_tl);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
             MyhealthySign myhealthySign = list.get(position);
             viewHolder.tv_sub_content.setText(TextUtils.isEmpty(myhealthySign.getContext()) ? "" :myhealthySign.getContext());
            viewHolder.tv_cu_tl.setText(TextUtils.isEmpty(myhealthySign.getTitle()) ? "" :myhealthySign.getTitle());
            return convertView;
        }

        private class ViewHolder{
            private TextView tv_sub_content,tv_cu_tl;
        }
    }


    private class MyhealthySign {
        private String title;
        private String context;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContext() {
            return context;
        }

        public void setContext(String context) {
            this.context = context;
        }
    }
}
